package com.idsmanager.zdh.web.controller.portal;

import com.idsmanager.zdh.service.UserService;
import com.idsmanager.oauth.service.dto.user.UserEditTableDto;
import com.idsmanager.oauth.service.dto.user.UserPaginated;
import com.idsmanager.oauth.service.dto.user.UserTableDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;

@Controller
@RequestMapping("user")
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(IdsFileController.class);

    @Autowired
    private UserService userService;

    /**
     * 分页展示用户
     * @param paginated
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(UserPaginated paginated, Model model) {
        paginated = userService.loadUserPaginated(paginated);
        model.addAttribute("paginated", paginated);
        return "user_list";
    }

    /**
     * 删除用户
     * @param uuid
     * @param model
     * @return
     */
    @RequestMapping("delete_{uuid}")
    public String delete(@PathVariable("uuid") String uuid, Model model) {
        userService.archiveUserByGuid(uuid);
        return "redirect:list";
    }

    /**
     * 根据uuid查询用户详细
     * @param uuid
     * @param model
     * @return
     */
    @RequestMapping("details_{uuid}")
    public String details(@PathVariable("uuid") String uuid, Model model) {
        UserEditTableDto userEditTableDto = userService.findByGuid(uuid);
        model.addAttribute("userEditTableDto", userEditTableDto);
        return "user_edit";
    }

    /**
     * 编辑保存用户
     * @param userEditTableDto
     * @param result
     * @param model
     * @return
     */
    @RequestMapping("eidt")
    public String eidt(@ModelAttribute("userEditTableDto")@Valid UserEditTableDto userEditTableDto, BindingResult result, Model model) {
        if(result.hasErrors()){
            return "user_edit";
        }
        userService.updateUser(userEditTableDto);
        model.addAttribute("userEditTableDto", userEditTableDto);
        return "redirect:list";
    }


//    @RequestMapping("list")
//    @ResponseBody
//    public BaseResp list(UserPaginated paginated, Model model) {
//        paginated = userService.loadUserPaginated(paginated);
//        model.addAttribute("paginated", paginated);
//        BaseResp baseResp = new BaseResp();
//        baseResp.setTotal((int) paginated.getTotalSize());
//        baseResp.setRows(paginated.getList());
//        return baseResp;
//    }
//
//    class BaseResp{
//        private int total;
//        private List<UserDto> rows;
//
//        public void setRows(List<UserDto> rows) {
//            this.rows = rows;
//        }
//
//        public void setTotal(int total) {
//            this.total = total;
//        }
//
//        public int getTotal() {
//            return total;
//        }
//
//        public List<UserDto> getRows() {
//            return rows;
//        }
//    }


    /**
     * 跳转至创建用户的form引导界面
     * @param model
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create(Model model) {
        model.addAttribute("userTableDto", new UserTableDto());
        return "user_create";
    }

    /**
     * 创建用户
     * @param userTableDto
     * @param result
     * @param model
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(@ModelAttribute("userTableDto") @Valid UserTableDto userTableDto, BindingResult result, Model model) {
        if(result.hasErrors()){
            return "user_create";
        }
        userService.createUser(userTableDto);
        return "redirect:list";
    }

    /**
     * 重置密码
     * @param uuid
     * @param model
     * @return
     */
    @RequestMapping(value = "resetpassword_{uuid}", method = RequestMethod.GET)
    public String resetpassword(@PathVariable("uuid") String uuid, Model model) {
        UserTableDto userTableDto = userService.resetpassword(uuid);
        model.addAttribute("userTableDto", userTableDto);
        return "user_resetpassword";
    }
}
